package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.util.Consumer;
import easier.app.x.service.BinderService;

public class MainActivity119 extends AppCompatActivity {

    private Button start;
    private Button doSomething;
    private Button stop;

    private MainActivity119ForeService mService;
    private Button startFore;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity119.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main119 );

        initView();
    }

    private void initView() {

        start = (Button) findViewById( R.id.start );
        doSomething = (Button) findViewById( R.id.doSomething );
        startFore = (Button) findViewById( R.id.startFore );
        stop = (Button) findViewById( R.id.stop );

        start.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                BinderService.bindService( v.getContext(), MainActivity119ForeService.class,
                    new Consumer< MainActivity119ForeService >() {
                        @Override
                        public void accept( MainActivity119ForeService mainActivity119ForeService ) {

                            mService = mainActivity119ForeService;
                        }
                    } );
            }
        } );

        doSomething.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                if ( mService != null ) {
                    mService.sayHello();
                }
            }
        } );

        stop.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                if ( mService != null ) {
                    mService.destroy();
                    mService = null;
                }
            }
        } );

        startFore.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                if ( mService != null ) {
                    mService.startFore();
                }
            }
        } );
    }
}